<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hystrix," />










<meta name="description" content="Hystrix原理详解">
<meta name="keywords" content="Hystrix">
<meta property="og:type" content="article">
<meta property="og:title" content="Hystrix原理">
<meta property="og:url" content="http://yoursite.com/2018/12/06/Hystrix原理/Hystrix原理/index.html">
<meta property="og:site_name" content="程序员乌托邦">
<meta property="og:description" content="Hystrix原理详解">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2018-12-06T09:10:03.712Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Hystrix原理">
<meta name="twitter:description" content="Hystrix原理详解">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2018/12/06/Hystrix原理/Hystrix原理/"/>





  <title>Hystrix原理 | 程序员乌托邦</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">程序员乌托邦</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/12/06/Hystrix原理/Hystrix原理/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Wolf、Heart">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="程序员乌托邦">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Hystrix原理</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-12-06T17:04:47+08:00">
                2018-12-06
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Hystrix/" itemprop="url" rel="index">
                    <span itemprop="name">Hystrix</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h3 id="Hystrix原理详解"><a href="#Hystrix原理详解" class="headerlink" title="Hystrix原理详解"></a>Hystrix原理详解</h3><a id="more"></a>
<h1 id="一、前言"><a href="#一、前言" class="headerlink" title="一、前言"></a>一、前言</h1><p>在分布式系统中，会涉及到一个服务会调用其它多个服务来获取最终的结果，例如商品详情服务会调用商品服务、价格服务、库存服务、营销服务、评价服务等来展示最终的结果。</p>
<p>如果其中某个服务，假如是评价服务出现问题，那么商品详情服务中会堆积大量请求评价服务的线程，导致系统线程数量飙升，占用系统资源，从而导致商品详情服务也变得不可用。</p>
<h1 id="二、服务雪崩定义"><a href="#二、服务雪崩定义" class="headerlink" title="二、服务雪崩定义"></a>二、服务雪崩定义</h1><p>当某个服务变得不可用，会影响跟它有关联的所有服务，从而导致服务大面积不可用，这种现象称为服务雪崩效应。</p>
<h1 id="三、服务雪崩原因"><a href="#三、服务雪崩原因" class="headerlink" title="三、服务雪崩原因"></a>三、服务雪崩原因</h1><ol>
<li>硬件故障</li>
<li>系统bug</li>
<li>缓存雪崩、缓存穿透</li>
<li>大量请求</li>
</ol>
<h1 id="四、服务雪崩应用策略"><a href="#四、服务雪崩应用策略" class="headerlink" title="四、服务雪崩应用策略"></a>四、服务雪崩应用策略</h1><ol>
<li>服务限流</li>
<li>服务动态扩容</li>
<li>服务调用者降级</li>
<li>改进缓存策略</li>
</ol>
<h3 id="服务限流"><a href="#服务限流" class="headerlink" title="服务限流"></a>服务限流</h3><ol>
<li>接入层限流（Nginx + Lua）</li>
<li>分布式限流（Redis + Lua）</li>
<li>应用层限流（RateLimiter）</li>
</ol>
<h3 id="服务扩容"><a href="#服务扩容" class="headerlink" title="服务扩容"></a>服务扩容</h3><p>增加机器</p>
<h3 id="服务调用者降级"><a href="#服务调用者降级" class="headerlink" title="服务调用者降级"></a>服务调用者降级</h3><p>使用Hystrix</p>
<h3 id="缓存策略"><a href="#缓存策略" class="headerlink" title="缓存策略"></a>缓存策略</h3><ol>
<li>缓存预热</li>
<li>异步刷新缓存</li>
</ol>
<h1 id="五、使用Hystrix预防雪崩"><a href="#五、使用Hystrix预防雪崩" class="headerlink" title="五、使用Hystrix预防雪崩"></a>五、使用Hystrix预防雪崩</h1><p>hystrix设计原则：</p>
<ol>
<li>资源隔离</li>
<li>熔断器</li>
<li>命令模式</li>
</ol>
<p>资源隔离：</p>
<ol>
<li>线程池隔离</li>
<li>信号量隔离</li>
</ol>
<p>熔断器：</p>
<p>默认情况下熔断器开关是关闭状态，当达到一定请求，产生一定的错误率，熔断器开关打开，一定时间内拒绝请求，过了指定的时间，允许放入一个请求进来，如果该请求执行成功，那么关闭熔断器，如果请求执行失败，那么熔断器继续保持打开状态</p>
<p>命令模式：</p>
<p>对于所有的服务依赖调用都使用command来包装</p>
<h1 id="六、Hystrix内部执行逻辑"><a href="#六、Hystrix内部执行逻辑" class="headerlink" title="六、Hystrix内部执行逻辑"></a>六、Hystrix内部执行逻辑</h1><ol>
<li>创建command对象</li>
<li>首先判断熔断器开关是否打开，如果处于打开状态，则执行fallback逻辑，如果处于关闭状态，则继续往下执行</li>
<li>判断分配的线程池是否足够，如果被拒绝，则执行fallback逻辑，如果不被拒绝，则继续执行</li>
<li>执行run()方法，也就是服务调用逻辑，如果执行失败或者执行超时，则执行fallback逻辑，否则返回执行结果</li>
</ol>
<p>==注意==：run()方法执行失败或者执行超时都会Metrics更新服务健康状况</p>
<h1 id="七、Hystrix属性解释"><a href="#七、Hystrix属性解释" class="headerlink" title="七、Hystrix属性解释"></a>七、Hystrix属性解释</h1><ol>
<li><p>默认采用线程池隔离方式</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">private static final ExecutionIsolationStrategy default_executionIsolationStrategy = ExecutionIsolationStrategy.THREAD;</span><br></pre></td></tr></table></figure>
</li>
<li><p>记录故障默认最小请求数，10s内发生20个请求才开始进行故障记录</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">private static final Integer default_circuitBreakerRequestVolumeThreshold = 20;</span><br></pre></td></tr></table></figure>
</li>
<li><p>默认故障率为50%，超过该概率才会打开熔断器</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">private static final Integer default_circuitBreakerErrorThresholdPercentage = 50;</span><br></pre></td></tr></table></figure>
</li>
<li><p>熔断器默认打开时间</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">private static final Integer default_circuitBreakerSleepWindowInMilliseconds = 5000;</span><br></pre></td></tr></table></figure>
</li>
<li><p>超时功能默认开启</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">private static final Boolean default_executionTimeoutEnabled = true;</span><br></pre></td></tr></table></figure>
</li>
<li><p>默认超时时间为1s</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">private static final Integer default_executionTimeoutInMilliseconds = 1000;</span><br></pre></td></tr></table></figure>
</li>
<li><p>默认核心线程数10</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">static int default_coreSize = 10;</span><br></pre></td></tr></table></figure>
</li>
<li><p>默认最大线程数</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">static int default_maximumSize = 10;</span><br></pre></td></tr></table></figure>
</li>
<li><p>默认队列大小-1，也就是不存放元素在队列中</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">static int default_maxQueueSize = -1;</span><br></pre></td></tr></table></figure>
</li>
</ol>
<h1 id="八、Hystrix参考文献"><a href="#八、Hystrix参考文献" class="headerlink" title="八、Hystrix参考文献"></a>八、Hystrix参考文献</h1><ul>
<li><a href="http://hot66hot.iteye.com/blog/2155036" target="_blank" rel="noopener">Hystrix 使用与分析</a></li>
<li><a href="https://segmentfault.com/a/1190000005988895" target="_blank" rel="noopener">防雪崩利器：熔断器 Hystrix 的原理与使用</a></li>
<li><a href="https://my.oschina.net/7001/blog/1619842" target="_blank" rel="noopener">Hystrix原理与实战</a></li>
<li><a href="https://my.oschina.net/lyyjason/blog/1800102" target="_blank" rel="noopener">hystrix参数详解</a></li>
</ul>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Hystrix/" rel="tag"># Hystrix</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2018/12/05/使用Guava实现缓存/使用Guava实现缓存/" rel="next" title="使用Guava实现缓存">
                <i class="fa fa-chevron-left"></i> 使用Guava实现缓存
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2018/12/06/Hystrix使用/Hystrix使用/" rel="prev" title="Hystrix使用">
                Hystrix使用 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">Wolf、Heart</p>
              <p class="site-description motion-element" itemprop="description">主要用于分享平时学习的只是以及遇到的困难，帮助自己以及需要的人一起共同成长</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">45</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">14</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">13</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#Hystrix原理详解"><span class="nav-number">1.</span> <span class="nav-text">Hystrix原理详解</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#一、前言"><span class="nav-number"></span> <span class="nav-text">一、前言</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#二、服务雪崩定义"><span class="nav-number"></span> <span class="nav-text">二、服务雪崩定义</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#三、服务雪崩原因"><span class="nav-number"></span> <span class="nav-text">三、服务雪崩原因</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#四、服务雪崩应用策略"><span class="nav-number"></span> <span class="nav-text">四、服务雪崩应用策略</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#服务限流"><span class="nav-number">1.</span> <span class="nav-text">服务限流</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#服务扩容"><span class="nav-number">2.</span> <span class="nav-text">服务扩容</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#服务调用者降级"><span class="nav-number">3.</span> <span class="nav-text">服务调用者降级</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#缓存策略"><span class="nav-number">4.</span> <span class="nav-text">缓存策略</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#五、使用Hystrix预防雪崩"><span class="nav-number"></span> <span class="nav-text">五、使用Hystrix预防雪崩</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#六、Hystrix内部执行逻辑"><span class="nav-number"></span> <span class="nav-text">六、Hystrix内部执行逻辑</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#七、Hystrix属性解释"><span class="nav-number"></span> <span class="nav-text">七、Hystrix属性解释</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#八、Hystrix参考文献"><span class="nav-number"></span> <span class="nav-text">八、Hystrix参考文献</span></a></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Wolf、Heart</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  











  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  

  
  
    <script type="text/javascript" src="/lib/canvas-nest/canvas-nest.min.js"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
